package edu.cicese.alz.swing;

import javax.swing.JPanel;
import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 2/08/12
 * Time: 01:06 PM
 */
public class TypeButtonListener extends MouseAdapter {

	private CustomToggleButton[] buttons;
	private int index;

	private JPanel panel;
	private String panelName;

	public TypeButtonListener(CustomToggleButton[] buttons, int index, JPanel panel, String panelName) {
		this.buttons = buttons;
		this.index = index;
		this.panel = panel;
		this.panelName = panelName;
	}

	public void mouseEntered(MouseEvent evt) {
		if (buttons[index].isEnabled() && !buttons[index].isSelected()) {
			buttons[index].paintRollover();
		}
	}

	public void mouseExited(MouseEvent evt) {
		if (!buttons[index].isSelected()) {
			buttons[index].setSelected(false);
		}
	}

	public void mousePressed(MouseEvent evt) {
		if (buttons[index].isEnabled()) {
			((CardLayout) panel.getLayout()).show(panel, panelName);
			for (int i = 0; i < buttons.length; i++) {
				buttons[i].setSelected(i == index);
			}
		}
	}
}
